# -*- coding: utf-8 -*-
import itertools

for s in itertools.repeat('hello world', 3): #第二个参数不提供，将无限循环
    print s
for n in itertools.count(10): #从10开始计数
    if n>=15:
        break
    print n
print list(itertools.islice(itertools.count(10), 5))
count = 0
for c in itertools.cycle('ABCD'):
    print c
    count += 1
    if count >= 10:
        break
    
print list(itertools.product('ab', '123'))
print list(itertools.product('abc', repeat=2))
#print list(itertools.accumulate(range(10), operator.add)) #python3
print list(itertools.chain(range(3), [7,8]))
print list(itertools.izip('ABCD', 'xy'))
print list(itertools.izip_longest('ABCD', [1,2,3], fillvalue='-'))

print list(itertools.permutations(range(1,4)))
print list(itertools.permutations(range(1,4), 2))
print list(itertools.combinations(range(1,5), 3))
print list(itertools.combinations_with_replacement('ABC', 2))

print list(itertools.compress('ABCDEF', [1,0,1,0,1,1])) #根据真值表过滤
print list(itertools.imap(pow, [2,3,4], [4,2,1]))
print list(itertools.starmap(pow, [(2,4),(3,2),(4,1)]))
print list(itertools.starmap(str.islower, 'aBCDefGhI'))

print list(itertools.takewhile(lambda x:x<5, [1,4,6,3,8])) #丢弃真值前面的元素
print list(itertools.dropwhile(lambda x:x<5, [1,4,6,3,8])) #相反
for condition,number in itertools.groupby(range(10), lambda x: x<5 or x>8):
    print condition, list(number)